package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.ShopChannel;
import com.caocaog.market.entity.SiteChannel;
import com.caocaog.market.service.IShopChannelService;
import com.caocaog.market.service.ISiteChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 分站频道信息表
 *
 * @author jeecg-boot
 * @date 2021-11-23
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "siteChannel", tags = "分站频道信息表")
@RequestMapping("/shop/siteChannel")
public class SiteChannelController extends JeecgController<SiteChannel, ISiteChannelService> {
    @Autowired
    private ISiteChannelService siteChannelService;
    @Autowired
    private IShopChannelService shopChannelService;

    /**
     * 分页列表查询
     *
     * @param siteChannel
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "分站频道信息表-分页列表查询")
    @ApiOperation(value = "分站频道信息表-分页列表查询", notes = "分站频道信息表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(SiteChannel siteChannel,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<SiteChannel> queryWrapper = QueryGenerator.initQueryWrapper(siteChannel, req.getParameterMap());
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isNotEmpty(loginUser.getUserType()) && 1 != loginUser.getUserType()) {
            //分站
            String siteId = loginUser.getSiteId();
            queryWrapper.eq("site_id", siteId);

        }
        Page<SiteChannel> page = new Page<SiteChannel>(pageNo, pageSize);
        IPage<SiteChannel> pageList = siteChannelService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param siteChannel
     * @return
     */
    @AutoLog(value = "分站频道信息表-添加")
    @ApiOperation(value = "分站频道信息表-添加", notes = "分站频道信息表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SiteChannel siteChannel) {
        String[] ids = siteChannel.getChannelId().split(",");
        for (String id : ids) {
            //check 只能选择二级频道
            ShopChannel shopChannel = shopChannelService.getById(id);
            if (oConvertUtils.isNotEmpty(shopChannel) && shopChannel.getParentId().equals("0")) {
                return Result.error("只能选择二级频道哟~");
            }
        }
        List<SiteChannel> siteChannels = new ArrayList<>();
        for (String id : ids) {

            //check 如果该分站已有该频道 跳过
            List<SiteChannel> check = siteChannelService.list(new QueryWrapper<SiteChannel>()
                    .eq("channel_id", id)
                    .eq("site_id", siteChannel.getSiteId()));
            if (oConvertUtils.listIsNotEmpty(check)) {
                continue;
            }
            SiteChannel siteChannelOne = new SiteChannel();
            siteChannelOne.setChannelId(id);
            siteChannelOne.setSiteId(siteChannel.getSiteId());
            siteChannelOne.setIsIndex(siteChannel.getIsIndex());
            siteChannelOne.setSequence(siteChannel.getSequence());
            siteChannels.add(siteChannelOne);
        }
        siteChannelService.saveBatch(siteChannels);
        return Result.OK("添加成功！", true);
    }

    /**
     * 编辑
     *
     * @param siteChannel
     * @return
     */
    @AutoLog(value = "分站频道信息表-编辑")
    @ApiOperation(value = "分站频道信息表-编辑", notes = "分站频道信息表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SiteChannel siteChannel) {
        String[] ids = siteChannel.getChannelId().split(",");
        if (ids.length > 1) {
            return Result.error("请选择一个频道进行修改哟~");
        }
        String id = ids[0];
        //check 只能选择二级频道
        ShopChannel shopChannel = shopChannelService.getById(id);
        if (oConvertUtils.isNotEmpty(shopChannel) && shopChannel.getParentId().equals("0")) {
            return Result.error("只能选择二级频道哟~");
        }
        //check 如果该分站已有该频道 跳过
        SiteChannel check = siteChannelService.getOne(new QueryWrapper<SiteChannel>()
                .eq("channel_id", id)
                .eq("site_id", siteChannel.getSiteId()).last("limit 1 "));
        if (oConvertUtils.isNotEmpty(check)) {
            return Result.error("分站下已有该频道~");
        }
        siteChannelService.updateById(siteChannel);
        return Result.OK("编辑成功!", true);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "分站频道信息表-通过id删除")
    @ApiOperation(value = "分站频道信息表-通过id删除", notes = "分站频道信息表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        siteChannelService.removeById(id);
        return Result.OK("删除成功!", true);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "分站频道信息表-批量删除")
    @ApiOperation(value = "分站频道信息表-批量删除", notes = "分站频道信息表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.siteChannelService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!", true);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "分站频道信息表-通过id查询")
    @ApiOperation(value = "分站频道信息表-通过id查询", notes = "分站频道信息表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SiteChannel siteChannel = siteChannelService.getById(id);
        if (siteChannel == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(siteChannel);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param siteChannel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SiteChannel siteChannel) {
        return super.exportXls(request, siteChannel, SiteChannel.class, "分站频道信息表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SiteChannel.class);
    }

}
